package org.jeecg.modules.collectarticles.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.collectarticles.entity.PhilCollectionArticles;
import org.jeecg.modules.collectarticles.mapper.PhilCollectionArticlesMapper;
import org.jeecg.modules.collectarticles.service.IPhilCollectionArticlesService;
import org.jeecg.modules.article.entity.Article;
import org.jeecg.modules.article.mapper.ArticleMapper;
import org.jeecg.modules.collectarticles.vo.CollectsVO;
import org.jeecg.modules.knowledge.entity.PhilKnowledge;
import org.jeecg.modules.knowledge.mapper.PhilKnowledgeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 用户收藏文章表
 * @Author: jeecg-boot
 * @Date:   2020-11-11
 * @Version: V1.0
 */
@Service
public class PhilCollectionArticlesServiceImpl extends ServiceImpl<PhilCollectionArticlesMapper, PhilCollectionArticles> implements IPhilCollectionArticlesService {

    @Autowired
    private PhilCollectionArticlesMapper collectionArticlesMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private PhilKnowledgeMapper philKnowledgeMapper;

//    @Override
    public Page<CollectsVO> queryByOpenId2(Page<CollectsVO> page, String openId) {
        // 获得该用户下所有的文章id
        List<PhilCollectionArticles> arrays = collectionArticlesMapper.selectList(page,openId);
        Map<Integer, List<PhilCollectionArticles>> collect = arrays.stream().collect(Collectors.groupingBy(PhilCollectionArticles::getItemType));
        List<PhilCollectionArticles> philCollectionKnowledges = collect.get(0);
        List<PhilCollectionArticles> philCollectionArticles = collect.get(1);
        List<CollectsVO> list = new ArrayList<>();
        // 该用户至少有一个收藏的文章
          if (philCollectionArticles!=null && ! philCollectionArticles.isEmpty()){
              List<CollectsVO> demo = articleMapper.queryArticleCollectByIds(philCollectionArticles);
              demo.forEach(x->x.setItemType(1));
              list.addAll(demo);

        }
          // 用户至少有一个收藏的知识
          if (philCollectionKnowledges!=null && ! philCollectionKnowledges.isEmpty()){
              List<CollectsVO> collectsVOS = philKnowledgeMapper.queryArticleCollectByIds(philCollectionKnowledges);
              list.addAll(collectsVOS);
          }

        List<CollectsVO> lis = list.stream()
                  .sorted(Comparator.comparing(CollectsVO::getCreateTime).reversed())
                  .collect(Collectors.toList());
        return page.setRecords(lis);
    }

    @Override
    public Page<CollectsVO> queryByOpenId(Page<CollectsVO> page, String openId) {
        // 获得该用户下所有的文章id
        List<CollectsVO> arrays = collectionArticlesMapper.selectList2(page,openId);
        arrays.forEach(item->{
            if (item.getItemType()==0) {
                PhilKnowledge philKnowledge = philKnowledgeMapper.selectById(item.getArticleId());
                item.setArticleAuthor(philKnowledge.getKnowledgeAuthor());
                item.setArticleTitle(philKnowledge.getKnowledgeTitle());
                item.setCreateTime(philKnowledge.getCreateTime());
            }
        });
        return page.setRecords(arrays);
    }
}
